package huu.phong.youtube;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;

import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

public class ColorSlider extends JComponent{
	private static final long serialVersionUID = 1495787053831058306L;
	Color rangeColor = new Color(230, 230, 230);
	Color normalColor = Color.WHITE;//new Color(240, 240, 240);
	Color valueColor = new Color(66, 174, 227);
	Color borderColor = new Color(204, 204, 204);
	Color rangeTextColor = new Color(163, 73, 164);
	private long range = 0;
	Configure configure;
	int rangeWidth = 0;
	int valueWidth = 0;
	int x = 1;
	int y = 0;
	long value = 0;
	long min = 0;
	long max = 100;
	int width = 220;
	int height = 30;
	EmbeddedMediaPlayer player;
	boolean dragged = false;
	long plusDuration = 0;
	boolean seeking = false;
	VideoToolTip toolTip = new VideoToolTip();
	
	public ColorSlider(EmbeddedMediaPlayer mediaPlayer){
		super();
		this.player = mediaPlayer;
		setPreferredSize(new Dimension(width, height));
		setMaximumSize(new Dimension(width, height));
		setMinimumSize(new Dimension(width, height));
		ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
        toolTipManager.setInitialDelay(0);
        toolTipManager.setDismissDelay(7200000);
//        toolTipManager.setLightWeightPopupEnabled(false);
        setOpaque(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        addMouseListener(new MouseAdapter() {
        	public void mousePressed(MouseEvent e) {
            	if (!SwingUtilities.isLeftMouseButton(e)) return;
            	value = valueAtPoint(e);
				repaint();
            	dragged = true;
            }
        	
        	public void mouseReleased(MouseEvent e){
				dragged = false;
				if (!SwingUtilities.isLeftMouseButton(e)) return;
				value = valueAtPoint(e);
				repaint();
				MouseEvent event = new MouseEvent(
                        ColorSlider.this,
                        MouseEvent.MOUSE_MOVED,
                        System.currentTimeMillis(),
                        0,
                        e.getX(),
                        e.getY(),
                        0,
                        false);
                ToolTipManager.sharedInstance().mouseMoved(event);
				new Thread(){
					public void run(){
						seeking = true;
						plusDuration = value;
						player.playMedia(player.mrl().replaceFirst("&begin=.*", "") + "&begin=" + plusDuration);
					}
				}.start();
			}
		});
	}
	
	public void setRange(long range){
		this.range = range;
		this.repaint();
	}
	
	public long getRange(){
		return range;
	}
	
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
		Color oldColor = g2.getColor();
		y = height / 2 - 3;
		g2.setColor(borderColor);
		g2.drawRect(0, y - 1, width - 1, 5);
		g2.setColor(normalColor);
		g2.fillRect(x, y, width - 2, 4);
		rangeWidth = (int) ((this.range * (width - 2)) / max);
		g2.setColor(rangeColor);
		if (rangeWidth > x) g2.fillRect(x, y, rangeWidth, 4);
		if(range < max && rangeWidth > 0){
			g2.setColor(rangeTextColor);
			if (rangeWidth + 23 > width){
				g2.drawString((this.range * 100 / max) + "%", width - 23, y - 2);
			}else{
				g2.drawString((this.range * 100 / max) + "%", rangeWidth, y - 2);
			}
		}
		valueWidth = (int) ((getValue() * (width - 2)) / max);
		g2.setColor(valueColor);
		if (valueWidth > x) g2.fillRect(x, y, valueWidth, 4);
		g2.setColor(oldColor);
	}
	
	public String durationAtMouse(MouseEvent e){
		return YouTubeAPI.toDuration(valueAtPoint(e) / 1000);
	}
	
	public long valueAtPoint(MouseEvent e){
		long value = (e.getPoint().x + 1) * max / width;
		if (value < 0) {
			return 0;
		}else if(value > max){
			return max;
		}
		return value;
	}
	
	public void setValue(long value){
		this.value = value + plusDuration;
		repaint();
	}
	
	public void setMax(long max){
		this.max = max;
	}
	
	public long getValue(){
		return this.value;
	}
	
	public JToolTip createToolTip(){
		return toolTip;
	}
	
	public Point getToolTipLocation(MouseEvent e){
		return new Point(e.getX() - (toolTip.getWidth() / 2), - toolTip.getPreferredSize().height);
	}
}
